<?php

App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PhotosController
 *
 * @author sirine
 */
class PhotosController extends AppController {

    var $name = 'Photos';
    public $components = array('Session');
    public $recherche = 'off'; //actvitié ou non
    public $cle; //mots cle de la recherche en cours
    public $helpers = array('Html', 'Js', 'Fancybox.Fancybox');

    //Ajouter des photos 
    public function addphotos($id) {
        if ($this->request->is('post')) {
            //recup data form
            $d = $this->request->data;
            //val par défauts
            $d['Photo']['id'] = null;
            $d['Photo']['ajoute_par_PHOTO'] = AuthComponent::user('username');
            $d['Photo']['STADES_id_STADE'] = $id;
            //req sql
            if ($this->Photo->save($d, true, array('url_PHOTO', 'tags_PHOTO', 'ajoute_par_PHOTO', 'STADES_id_STADE'))) {
                $this->Session->setFlash(__('Your post has been saved.'));
                return $this->redirect(array('controller' => 'stades', 'action' => 'infostade', $id));
            } else {
                $this->Session->setFlash(__('Unable to add your photo.'));
            }
        } else if ($id = null) {
            $this->redirect('/');
        }
    }

    //Récupérer les photos d'un seul stade
    function portofolio($id) {
        if ($this->recherche == 'on')
            return $this->Photo->find('all', array('conditions' => array('STADES_id_STADE' => $id, 'tags_PHOTO LIKE' => '%' . $this->cle . '%')));
        else
            return $this->Photo->find('all', array('conditions' => array('STADES_id_STADE' => $id)));
    }

    function activerecherche($param) {
        $this->recherche = 'on';
        $this->cle = $param;
    }

    function desactiverecherche() {
        debug('ici');
        $this->recherche = 'off';
        $this->cle = '';
    }

    //filtrer selon tag
    function rechercher($keys) {
        return $this->Photo->find('all', array('fields' => 'STADES_id_STADE', 'conditions' => array('tags_PHOTO LIKE' => '%' . $keys . '%')));
    }

}
